package mn.idax.exchange.otc.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import mn.idax.exchange.otc.domain.SimpleUserLanguage;
import mn.idax.exchange.otc.domain.UserLanguageRequest;
import mn.idax.exchange.otc.entity.UserLanguage;
import mn.idax.exchange.otc.entity.UserLanguageExample;
import mn.idax.exchange.otc.mapper.UserLanguageMapper;
import mn.idax.exchange.otc.service.UserLanguageService;
import mn.idax.exchange.otc.util.DateUtils;
import mn.idax.exchange.otc.util.LogUtils;
import org.apache.http.util.TextUtils;
import org.eclipse.jetty.websocket.common.util.TextUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

/**
 * @Author: jack_huang
 * @Date: 2019/1/3 17:53
 */
@Service
public class UserLanguageServiceImpl implements UserLanguageService {

    @Autowired
    private UserLanguageMapper userLanguageMapper;

    /**
     * 查询 一个用户 一个设备号只有一条记录
     * @param userLanguageRequest
     * @return
     */
    @Override
    public UserLanguage queryLanguage(UserLanguageRequest userLanguageRequest) {
        UserLanguageExample example = new UserLanguageExample();
        List<String> machineids = Lists.newArrayList();
        machineids.add(userLanguageRequest.getMachineid().toLowerCase());
        machineids.add(userLanguageRequest.getMachineid().toUpperCase());
        example.createCriteria()
                .andUseridEqualTo(userLanguageRequest.getUserid())
                .andUsersourceEqualTo(userLanguageRequest.getUsersource())
//                .andMachineidEqualTo(userLanguageRequest.getMachineid());
                .andMachineidIn(machineids);
        List<UserLanguage> userLanguages = userLanguageMapper.selectByExample(example);
        if (userLanguages.isEmpty()){
            return null;
        }
        return userLanguages.get(0);
    }

    @Override
    public void insertLanguage(UserLanguageRequest userLanguageRequest) {
        UserLanguage userLanguage = new UserLanguage();
        BeanUtils.copyProperties(userLanguageRequest,userLanguage);
        userLanguage.setCreatetime(DateUtils.getUTCNow());
        userLanguage.setUpdatetime(userLanguage.getCreatetime());
        int insert = userLanguageMapper.insertSelective(userLanguage);
        LogUtils.info(this.getClass(),"userLanguage insert total" + String.valueOf(insert) + "insert params is" + JSON.toJSONString(userLanguage));

    }

    @Override
    public void updateLanguage(UserLanguageRequest userLanguageRequest) {
        UserLanguage userLanguage = new UserLanguage();
        BeanUtils.copyProperties(userLanguageRequest,userLanguage);
        userLanguage.setUpdatetime(DateUtils.getUTCNow());
        UserLanguageExample example = new UserLanguageExample();
        example.createCriteria()
                .andUseridEqualTo(userLanguageRequest.getUserid())
                .andUsersourceEqualTo(userLanguageRequest.getUsersource())
                .andMachineidEqualTo(userLanguageRequest.getMachineid());

        int update = userLanguageMapper.updateByExampleSelective(userLanguage,example);
        LogUtils.info(this.getClass(),"userLanguage update total" + String.valueOf(update) + "update params is" + JSON.toJSONString(userLanguage));

    }

    /**
     * 获取用户app上最近的一个语言
     * @param userId
     * @return
     */
    @Override
    public List<UserLanguage> queryLanguageByUserId(Integer userId) {
        return userLanguageMapper.queryLanguageByUserId(userId);
    }

    @Override
    public List<SimpleUserLanguage> getBuyerAndSellerSimpleLanguage(Integer buyer, Integer seller) {

        List<SimpleUserLanguage> buyerAndSellerSimpleLanguage = userLanguageMapper.getBuyerAndSellerSimpleLanguage(buyer, seller);
        for (SimpleUserLanguage simpleUserLanguage : buyerAndSellerSimpleLanguage) {
            if (Objects.isNull(simpleUserLanguage.getLanguage())){
                simpleUserLanguage.setLanguage("en_us");
            }
        }
        return buyerAndSellerSimpleLanguage;
    }

    @Override
    public SimpleUserLanguage getUsererSimpleLanguage(Integer userId) {
        SimpleUserLanguage usererSimpleLanguage = userLanguageMapper.getUsererSimpleLanguage(userId);
        if (Objects.isNull(usererSimpleLanguage.getLanguage())){
            usererSimpleLanguage.setLanguage("en_us");
        }
        return usererSimpleLanguage;
    }
}
